package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_用户_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/member")
public class SundayMemberController extends BaseController {
    @Autowired
    private SundayMemberService memberService;
    /**
     * 用户  主页
     * @param request
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request){
        return "/sunday/member/index";
    }

    /**
     * 分页用户信息
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                        HttpServletResponse response
    ) {
        try {
            PageResult<SundayMember> result=memberService.selectMember(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 用户详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,

            Model model){

        SundayMember member = memberService.getById(id);
        model.addAttribute("member", member);
            return "/sunday/member/input";
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam(value="id",required=true) Long id
    ) {
        try {
            memberService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/member/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }


    /**
     * 用户
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,
                          @RequestParam(value="orderId",required=false)Long orderId,
                          @RequestParam(value="userType",required=false)Integer userType,
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(LongUtil.isNotZreo(orderId)){
            model.addAttribute("orderId",orderId);
        }else {
            model.addAttribute("orderId",0);
        }
        if(userType!=null){
            model.addAttribute("userType",userType);
        }
        if(isSingle==null){
            return "/sunday/member/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/member/choose/choose_single";
        }else if(isSingle==0){
            return "/sunday/member/choose/choose_muilt";
        }

        return "/finance/member/choose/choose_erro";
    }

}
